package com.bjpowernode.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.commons.BaseApplication;
import com.bjpowernode.commons.DicEnum;
import com.bjpowernode.model.TDicValue;
import com.bjpowernode.model.TProduct;

import java.util.List;
import java.util.Objects;

public class IntentionProductConverter implements Converter<Object> {

    /**
     * 把Excel中读到的数据转换成java的数据
     *
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Object data = cellData.getStringValue(); //汽车之家，百度，视频直播
        List<TProduct> tProductList = (List<TProduct>) BaseApplication.cahceMap.get(DicEnum.PRODUCT.getCode());
        for (TProduct tProduct : tProductList) {
            Integer id = tProduct.getId();
            String name = tProduct.getName();

            if (Objects.equals(data, name)) {
                return id;
            }
        }
        return -1;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return Converter.super.convertToExcelData(value, contentProperty, globalConfiguration);
    }
}
